<?php
// Example
if(file_exists("config.xml")) {
	$config_xml=simplexml_load_file("config.xml");
} else {
	exit("Konnte Datei nicht laden. ");
}
if(isset($_GET["scene"])) {
	$sceneID=$_GET["scene"];
} else {
	$sceneID='005';
}
// $quelltext = file_get_contents("http://192.168.178.1:55000/cmd=IT A 1 ON");
// Pfad für Ligthmanager setzen
$path="/config/General/LightManager";
$LMPathXML=$config_xml->xpath($path);
$address=$LMPathXML[0]->URL;
$port=$LMPathXML[0]->PORT;
$path='/config/Scenes/Scene[@sortID=\''.$sceneID.'\']';
$ScenesXML=$config_xml->xpath($path);
$cmd = "";
$result = "";

$fp=fsockopen($address, (int) $port, $errno, $errstr);

if($fp) {
// Read welcome string
//	echo fread($fp,64);
//	echo 
	// Write command
	foreach($ScenesXML[0]->Step as $Step) {
		$actorID=(string)$Step->Actor;
		$action=(string)$Step->Action;
		if($actorID!='WAIT') {
			$path='/config/Actors/Actor[@ID=\''.$actorID.'\']';
			$actor_xml=$config_xml->xpath($path);
			
			$typ=$actor_xml[0]->Typ;
			$code=$actor_xml[0]->Code;
			$num=$actor_xml[0]->Num;
			$cmd=$cmd . $typ.' '.$code.' '.$num.' '.$action . '; ';
		} else {
			$cmd=$cmd .'WAIT '.$action . '; ';
		}

		// Finally read command prompt
	}
	fwrite($fp,$cmd);	
	fwrite($fp,"\n");
	// Return result
	echo $cmd;
	echo fread($fp,16), PHP_EOL;
	fclose($fp);
} else {
	$result = "Konnte keine Verbindung zum LM herstellen";	
}
echo $result;
?>